package com.osdp.common.sercurity.service;


import com.osdp.common.exception.PlatformException;
import com.osdp.common.sercurity.domain.Principal;

public interface ITokenIdentifyService {
    /**
     * 验证token
     * @param token
     * @return
     */
    boolean verify(String token);

    /**
     * 刷新token
     * @param token
     * @return
     */
    boolean refresh(String token);

    /**
     * 获取登录主体
     * @param token
     * @return
     */
    Principal getPrincipal(String token);

    default Principal checkAndGetPrincipal(String token) {
        if(!verify(token)){
            throw new PlatformException("token 认证失败");
        }
        if(!refresh(token)){
            throw new PlatformException("token 刷新失败");
        }
        Principal principal = getPrincipal(token);
        if(null == principal){
            throw new PlatformException("token 获取认证用户失败");
        }
        return principal;
    }
}
